package cars

import (
	"errors"
	"fmt"
	"parking-lot-go-admin/model"
	"strconv"
	"time"
)

type CarsOutStruct struct {
	Id             int       `gorm:"type:int(11) auto_increment; NOT NULL; primary_key"`
	ParkCode       string    `gorm:"type:varchar(255); COMMENT:'停车场码'"`
	ParkName       string    `gorm:"type:varchar(255); COMMENT:'停车场名称'"`
	CarNumber      string    `gorm:"type:varchar(255); COMMENT:'车牌号'"`
	InTime         time.Time `gorm:"type:datetime; COMMENT:'入场时间'"`
	InPhoto        string    `gorm:"type:varchar(255); COMMENT:'入场图片'"`
	OutTime        time.Time `gorm:"type:datetime; COMMENT:'出场时间'"`
	OutPhoto       string    `gorm:"type:varchar(255); COMMENT:'出场图片'"`
	RegionCode     string    `gorm:"type:varchar(255); COMMENT:'出场区域编号'"`
	RegionName     string    `gorm:"type:varchar(255); COMMENT:'出场区域名称'"`
	ParkTime       int       `gorm:"type:int(11); COMMENT:'停车时长,单位：分钟'"`
	ParkAmt        int       `gorm:"type:int(11); COMMENT:'停车费用，单位：分'"`
	CouponAmt      int       `gorm:"type:int(11); COMMENT:'停车优惠，单位：分'"`
	ChargeAmt      int       `gorm:"type:int(11); COMMENT:'实收金额，单位：分'"`
	SerialNo       string    `gorm:"type:varchar(255); COMMENT:'出场流水号'"`
	Otype          int       `gorm:"type:int(11); COMMENT:'结算方式（企业1，个人2，混合3）'"`
	AmountPlatform int       `gorm:"type:int(11); COMMENT:'平台扣款，单位：分'"`
	AmountUser     int       `gorm:"type:int(11); COMMENT:'个人结算金额，单位：分'"`
	CreatedAt      time.Time `gorm:"type:datetime"`
	UpdatedAt      time.Time `gorm:"type:datetime"`
	CarNoColor     int       `gorm:"type:tinyint(1); COMMENT:'车牌颜色'"`
	MuchId         string    `gorm:"type:varchar(255)"`
	OrderNo        string    `gorm:"type:varchar(255)"`
	OrderType      int       `gorm:"type:tinyint(1); COMMENT:'订单类型'"`
	CarType        int       `gorm:"type:tinyint(1); COMMENT:'车辆类型'"`
	PushStatus     int       `gorm:"type:tinyint(1); COMMENT:'推送状态'"`
	PushErrNum     int       `gorm:"type:tinyint(1); COMMENT:'失败次数'"`
	PushTime       int       `gorm:"type:tinyint(1); COMMENT:'下次推送时间'"`
	IsAdmin        int       `gorm:"type:tinyint(1); COMMENT:'人工操作'"`
}

type ChargeAmt struct {
	Day   string
	Count int
}

func (c *CarsOutStruct) TableName() string {
	return "cars_out"
}

// 今日流水
func (c *CarsOutStruct) TodayFlowingWater() (int, error) {

	//查询出场记录
	db := model.Db

	//时间格式化
	todayFormat := time.Now().Format("2006-01-02 00:00:00")

	//金额
	var totalAmount float64
	if err := db.
		Table(c.TableName()).
		Where("out_time >= ?", todayFormat).
		Select("COALESCE(SUM(charge_amt), 0)").
		Scan(&totalAmount).
		Error; err != nil {
		return 0, errors.New(fmt.Sprintf("查询今日流水错误:%s", err.Error()))
	}

	totalAmountStr := fmt.Sprintf("%2.f", totalAmount/100)
	totalAmountInt, _ := strconv.Atoi(totalAmountStr)

	return totalAmountInt, nil
}

// 区间流水
func (c *CarsOutStruct) SectionFlowingWater() ([]string, error) {
	db := model.Db

	var strArr []string

	// 获取当前时间
	now := time.Now()

	// 计算8天前的时间
	eightDaysAgo := now.AddDate(0, 0, -8)

	// 设置循环开始时间为8天前
	currentTime := eightDaysAgo

	// 循环遍历从8天前到昨天的时间
	for currentTime.Before(now.AddDate(0, 0, -1)) {
		findStartTime := currentTime.Format("2006-01-02 00:00:00")
		findStartDate := currentTime.Format("2006-01-02")

		//在指定日期之前进场 并且在指定日期后出场
		var totalAmount float64
		if err := db.
			Table(c.TableName()).
			Where("in_time <= ?", findStartTime).
			Where("DATE(out_time) = ?", findStartDate).
			Select("COALESCE(SUM(charge_amt), 0)").
			Scan(&totalAmount).Error; err != nil {
			return strArr, errors.New(fmt.Sprintf("查询出场错误:%s", err.Error()))
		}

		totalAmountFloat := fmt.Sprintf("%2.f", totalAmount/100)
		strArr = append(strArr, totalAmountFloat)

		// 增加一天
		currentTime = currentTime.AddDate(0, 0, 1)
	}
	return strArr, nil
}
